package com.st.handler;

import cn.hutool.json.JSONUtil;
import com.st.common.security.SwordUserDetails;
import com.st.common.util.ResponseE;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author shent
 */
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        SwordUserDetails userDetails = (SwordUserDetails) authentication.getPrincipal();
        //todo 记录退出系统日志
        logWrite(userDetails);
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        httpServletResponse.setHeader("Content-Type", "application/json");
        httpServletResponse.getWriter().print(JSONUtil.toJsonStr(ResponseE.ok(null, "logout_successfully")));
    }

    private void logWrite(UserDetails userDetails) {
    }
}
